package com.bookmarket.controller;

import com.bookmarket.entity.Order;
import com.bookmarket.view.ExcelExportService;
import com.bookmarket.view.ExcelView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/FileExprot")
public class HJHFileExportController {


    //注意，使用lambda默认的java1.5不支持，必须告诉他为1.8，在pom文件中
    @SuppressWarnings("unchecked")//发送一条指令使警告沉默
    private ExcelExportService ExportService(){
        //lambda -> java 1.8之后的一种新的写法 实现接口 //参数别忘记了
        return (Map<String,Object> model, Workbook workbook) -> {
            List<Map> orderList = (List<Map>) model.get("orderList");
            //申明生成表的名称
            Sheet sheet = workbook.createSheet("订单表");
            //sheet 指的是生成的表 createRow()方法创建行数
            // title指的是表的第一行 createCell()创建列 setCellValue()方法用力啊赋值
            Row title = sheet.createRow(0);
            title.createCell(0).setCellValue("订单编号");

            title.createCell(1).setCellValue("收货人姓名");
            title.createCell(2).setCellValue("联系方式");
            title.createCell(3).setCellValue("收货地址");
            title.createCell(4).setCellValue("用户昵称");
            title.createCell(5).setCellValue("发货状态(0:未发 1:发货 2:收货 3:申请退货4.退货)");
            title.createCell(6).setCellValue("日期");
            for (int i = 0; i < orderList.size(); i++) {
                int rowIndex = i + 1;
                Row row = sheet.createRow(rowIndex);
                row.createCell(0).setCellValue((String) orderList.get(i).get("order_list"));
                row.createCell(1).setCellValue((String) orderList.get(i).get("rname"));
                row.createCell(2).setCellValue((String) orderList.get(i).get("phone"));
                row.createCell(3).setCellValue((String) orderList.get(i).get("address"));
                row.createCell(4).setCellValue((String) orderList.get(i).get("name"));
                row.createCell(5).setCellValue(orderList.get(i).get("status").toString());
                SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                java.util.Date date = new Date(((Double)orderList.get(i).get("createTime")).longValue());
                String str = sdf.format(date);
                row.createCell(6).setCellValue(str);
                System.out.println((String) orderList.get(i).get("rname"));

            }
            //System.out.println(orderList);
        };
    }

    @RequestMapping("/export")
    public ModelAndView exprot(String OrderList) {
        ModelAndView mv=new ModelAndView();
        //定义Excel视图 重写exportService方法
        ExcelView ev=new ExcelView(ExportService());

        //设置文件名
        ev.setFileName("订单表.xls");

        //System.out.println(OrderList);
        List<Map> list = new Gson().fromJson(OrderList, new TypeToken<ArrayList<Map>>() {
        }.getType());

        mv.addObject("orderList",list);
        mv.setView(ev);//相当于转发，把roleList的值转发过去
        return mv;
    }


    //导出表excel案例1
    @RequestMapping("/export1")
    public ModelAndView exprot1(){
        ModelAndView mv=new ModelAndView();
        //定义Excel视图 重写exportService方法
        ExcelView ev=new ExcelView(ExportService());

        //设置文件名
        ev.setFileName("所有角色.xls");

        Order order1=new Order();
        order1.setId(1);
        order1.setOrder_list("武松");
        order1.setRename("行者");
        Order order2=new Order();
        order2.setId(2);
        order2.setOrder_list("武松2");
        order2.setRename("行者3");
        Order order3=new Order();
        order3.setId(3);
        order3.setOrder_list("武松3");
        order3.setRename("行者4");

        List<Order> orderList=new ArrayList<>();
        orderList.add(order1);
        orderList.add(order2);
        orderList.add(order3);

        mv.addObject("orderList",orderList);
        mv.setView(ev);//相当于转发，把roleList的值转发过去
        return mv;
    }

/* //案例2
@SuppressWarnings("unchecked")//发送一条指令使警告沉默
private ExcelExportService exportService(){
    //lambda -> java 1.8之后的一种新的写法 实现接口 //参数别忘记了
    return (Map<String,Object> model, Workbook workbook) -> {
        List<Admin> roleList = (List<Admin>) model.get("roleList");
        //申明生成表的名称
        Sheet sheet = workbook .createSheet("所有角色");
        //sheet 指的是生成的表 createRow()方法创建行数
        // title指的是表的第一行 createCell()创建列 setCellValue()方法用力啊赋值
        Row title = sheet.createRow(0);
        title.createCell(0).setCellValue("编号");
        title.createCell(1).setCellValue("名称");
        title.createCell(2).setCellValue("备注");

        //遍历数据

        for (int i = 0; i < roleList.size(); i++) {
            Admin role = roleList.get(i);
            int rowIndex = i + 1;
            Row row = sheet.createRow(rowIndex);
            row.createCell(0).setCellValue(role.getId());
            row.createCell(1).setCellValue(role.getName());
            row.createCell(2).setCellValue(role.getPass());
        }
    };
}

    @RequestMapping("/export")
    public ModelAndView exprot(){
        ModelAndView mv=new ModelAndView();
        //定义Excel视图 重写exportService方法
        ExcelView ev=new ExcelView(exportService());

        //设置文件名
        ev.setFileName("所有角色.xls");

        Admin role1=new Admin();
        role1.setId(1);
        role1.setName("武松");
        role1.setPass("行者");
        Admin role2=new Admin();
        role2.setId(2);
        role2.setName("武松2");
        role2.setPass("行者3");
        Admin role3=new Admin();
        role3.setId(3);
        role3.setName("武松3");
        role3.setPass("行者4");

        List<Admin> roleList=new ArrayList<>();
        roleList.add(role1);
        roleList.add(role2);
        roleList.add(role3);

        mv.addObject("roleList",roleList);
        mv.setView(ev);//相当于转发，把roleList的值转发过去
        return mv;
    }*/
}